<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
<head>
    <meta charset="UTF-8">
    <title>填写邮寄地址</title>
</head>
<link type="text/css" rel="stylesheet"  th:href="@{/css/admin/bootstrap/css/bootstrap.css}" >
<link type="text/css" rel="stylesheet"  th:href="@{/css/sweetalert.css}" >
<script th:src="@{/js/sweetalert-dev.js}"></script>
<script th:src="@{/js/alipayjsapi.js}"></script>
<script th:src="@{/js/selectAddress.js}"></script>
<script th:src="@{/js/jquery-1.11.3.min.js}"></script>
<body style="background-color: #f2f1f7">
<div style="text-align: center;overflow: hidden;background-color: #fff">
    <img th:src="@{/images/banner.jpeg}" alt="" style="width: 100%">
</div>
<div style="padding: 20px;background-color: #fff;margin-top: 1px;">
    <button class="btn btn-block btn-lg btn-warning" id="alipayAddress">获取淘宝地址</button>
    <div class="form-group" style="margin-top: 10px">
        <label for="userName">联系人<span style="color: red">*</span></label>
        <input type="text" class="form-control" id="userName" placeholder="请填写联系人姓名">
    </div>
    <div class="form-group">
        <label for="userPhone">手机号码<span style="color: red">*</span></label>
        <input type="text" class="form-control" id="userPhone" placeholder="请填写手机号码">
    </div>
    <div class="form-group">
        <label for="userAddress">详细地址<span style="color: red">*</span></label>
        <textarea type="text" class="form-control" id="userAddress" placeholder="请填写详细地址"></textarea>
    </div>
    <span style="margin-bottom: 3px;font-size: 12px;">备注：无法保存，请使用支付宝扫一扫打开二维码！</span><br/>
    <button class="btn btn-block btn-lg btn-success" id="J_btn">保存</button>
</div>
<input id="uid" type="hidden" th:value=${uid}>
<input id="pid" type="hidden" th:value=${pid}>
<script>
    const alipayAddress = document.querySelector('#alipayAddress');
    alipayAddress.addEventListener('click',function () {
        am.selectAddress(function (data) {
            document.getElementById('userName').value = data.fullname;
            document.getElementById('userPhone').value = data.mobilePhone;
            document.getElementById('userAddress').value = data.prov + data.city + data.area + data.address;
        })
    });
    const btn = document.querySelector('#J_btn');

    btn.addEventListener('click', function () {
        const userName = document.getElementById('userName').value;
        const userPhone = document.getElementById('userPhone').value;
        const userAddress = document.getElementById('userAddress').value;
        const uid = document.getElementById('uid').value;
        const pid = document.getElementById('pid').value;
        if (userName === '' || userName === undefined || userName == null) {
            swal("请填写联系人姓名");
        } else if (userPhone === '' || userPhone === undefined || userPhone == null) {
            swal("请填写手机号码");
        } else if (checkPhone(userPhone)) {
            swal("手机号码格式不正确",userPhone);
        } else if (userAddress === '' || userAddress === undefined || userAddress == null) {
            swal("请填写邮寄地址");
        } else {
            window.sessionStorage.setItem("receiveName", userName);
            window.sessionStorage.setItem("receiveMobile", userPhone);
            window.sessionStorage.setItem("receiveAddress", userAddress);
            window.sessionStorage.setItem("uid", uid);
            window.sessionStorage.setItem("pid", pid);
            $.ajax({
                url: "/pay/credit/product/detail",
                type: "post",
                dataType: "json",
                data: {
                    uid:uid,
                    pid:pid
                },
                cache: false,
                async: false,
                success: function(data) {
                    if(data.code === "00000000"){
                        window.sessionStorage.setItem("productPrice", data.data.price);
                    }else{
                        ap.alert(data.msg);
                        $("#J_btn").attr("disabled","true");
                    }
                }
            })
            location.href = "confirm.html";
        }
    });
    function checkPhone(phone) {
        if (!(/^1[3456789]\d{9}$/.test(phone))) {
            return true;
        }
    }
    function parseUrl() {
        var searchHref = window.location.search.replace('?', '');
        var params = searchHref.split('&');
        var returnParam = {};
        params.forEach(function(param) {
            var paramSplit = param.split('=');
            returnParam[paramSplit[0]] = paramSplit[1];
        });
        return returnParam;
    }
</script>
</body>
</html>